package com.scitc.pleasurevideo;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.scitc.pleasurevideo.adapter.TvSetMenuPlayGridViewAdapter;
import com.scitc.pleasurevideo.base.ETActivity;
import com.scitc.pleasurevideo.data.HistoryTvDataMan;
import com.scitc.pleasurevideo.helper.ForwardHelper;
import com.scitc.pleasurevideo.imgutil.ImageManager;
import com.scitc.pleasurevideo.pojo.CachingTvVo;
import com.scitc.pleasurevideo.pojo.DoubanTvVo;
import com.scitc.pleasurevideo.pojo.MenuTvVo;
import com.scitc.pleasurevideo.pojo.PlayMenuTvVo;
import com.scitc.pleasurevideo.util.AtyContainer;
import com.scitc.pleasurevideo.util.GetHtmlContentUtil;
import com.scitc.pleasurevideo.util.SearchTvUtil;

import java.util.ArrayList;
import java.util.List;

public class TvPlayMenuActivity extends ETActivity {


    private TvSetMenuPlayGridViewAdapter madapter;
    private List<PlayMenuTvVo> items = new ArrayList<>();
    private Context mcontext;

    private MenuTvVo menuTvPojo;
    private DoubanTvVo doubanTvPojo;
    private RelativeLayout nameLayout;
    private ImageView igBanner;
    private LinearLayout layout1;
    private TextView title;
    private TextView tvNum;
    private TextView content;
    private ImageView downImg;
    private GridView mGridView;
    private Boolean isHeadImg = false;
    private String mGridViewClickType = "null";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        StatusBarUtil.setTranslucent(mInstance, 0);

        setContentView(R.layout.activity_tv_play_menu);

        mcontext = this;

        menuTvPojo = (MenuTvVo) getIntent().getSerializableExtra("menuTvPojo");

        doubanTvPojo = (DoubanTvVo) getIntent().getSerializableExtra("doubanTvPojo");

        initView();


        List<PlayMenuTvVo> temps = (List<PlayMenuTvVo>) getIntent().getSerializableExtra("items");
        if (temps == null || temps.size() < 1) {
            load();
        } else {
            madapter.addAll(temps);
        }

        click();

    }

    private void click() {

        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                if ("null".equals(mGridViewClickType)) {
                    mGridViewClickType = "click";
                } else {
                    return;
                }

                PlayMenuTvVo playMenuTvPojo = madapter.getItem(i);

                CachingTvVo cachingTvVo = new CachingTvVo();
                cachingTvVo.setImgUrl(doubanTvPojo.getCover());
                cachingTvVo.setAllSet("共" + madapter.getCount() + "集");
                cachingTvVo.setPlaySet("看到:第" + (i + 1) + "集");
                cachingTvVo.setName(doubanTvPojo.getTitle());
                cachingTvVo.setUrl(menuTvPojo.getUrl());
                cachingTvVo.setPalyListUrl(menuTvPojo.getPlayerString());
                cachingTvVo.setPlayMenuTvPojos(madapter.getItmes());
                HistoryTvDataMan.getHistoryTvDataMan().saveHistoryTvDataMan(cachingTvVo);

                ForwardHelper.toWebviewPlayActivity((Activity) mcontext, playMenuTvPojo.getPlayWebUrl(), doubanTvPojo.getTitle());
            }
        });

    }

    private void load() {
        if (!isHeadImg) {
            SearchTvUtil.searchTvImg(mcontext, menuTvPojo.getUrl() + menuTvPojo.getPlayerString());
        }
        SearchTvUtil.setOnSecrchName(new SearchTvUtil.OnSecrchName() {
            @Override
            public void setOnSearchName(String name) {

            }

            @Override
            public void setOnSearchImg(String url) {
                if (!isHeadImg) {
                    ImageManager.bindImageCenterCrop(mcontext, igBanner, url);
                    doubanTvPojo.setCover(url);
                    isHeadImg = true;
                }
            }
        });
//        自己手写的爬虫,爬取结果只有web播放地址
//        PlaySetMenuUtil.getPlayMenuTvList(mcontext, menuTvPojo.getUrl() + menuTvPojo.getPlayerString());
//
//        PlaySetMenuUtil.getPlayMenuTvList(new PlaySetMenuUtil.GetPlayMenuTvList() {
//            @Override
//            public void playmenuTvList(List<PlayMenuTvPojo> playMenuTvPojoList) {
//                madapter.addAll(playMenuTvPojoList);
//                tvNum.setText(playMenuTvPojoList.size()+"集");
//            }
//
//            @Override
//            public void playmenuTv(PlayMenuTvPojo playMenuTvPojo) {
//
//                madapter.addItem(playMenuTvPojo);
//                tvNum.setText(madapter.getCount()+"集");
//            }
//        });

//        使用jsoup爬取 能获取  web播放地址 m3u8播放地址 MP4播放地址 只要有
        GetHtmlContentUtil.getPlayMenuTvList(mcontext, menuTvPojo.getUrl() + menuTvPojo.getPlayerString());
        GetHtmlContentUtil.getPlayMenuTvList(new GetHtmlContentUtil.GetPlayMenuTvList() {
            @Override
            public void playmenuTvList(List<PlayMenuTvVo> playMenuTvPojoList) {
                madapter.addAll(playMenuTvPojoList);
                tvNum.setText(playMenuTvPojoList.size() + "集");
            }

            @Override
            public void playmenuTv(PlayMenuTvVo playMenuTvPojo) {
                madapter.addItem(playMenuTvPojo);
                tvNum.setText(madapter.getCount() + "集");
            }
        });
    }

    private void initView() {
        nameLayout = (RelativeLayout) findViewById(R.id.nameLayout);
        igBanner = (ImageView) findViewById(R.id.iv_banner);
        layout1 = (LinearLayout) findViewById(R.id.layout1);
        title = (TextView) findViewById(R.id.title);
        tvNum = (TextView) findViewById(R.id.tv_num);
        content = (TextView) findViewById(R.id.content);
        downImg = (ImageView) findViewById(R.id.down_img);
        mGridView = (GridView) findViewById(R.id.xGridView);
        mGridView.setSelector(new ColorDrawable(Color.TRANSPARENT));

        madapter = new TvSetMenuPlayGridViewAdapter(this, items);

        mGridView.setAdapter(madapter);


        if (TextUtils.isEmpty(doubanTvPojo.getCover()) ||
                doubanTvPojo.getCover().toLowerCase().equals("null".toLowerCase()) ||
                doubanTvPojo.getCover().trim().length() < 1) {
            isHeadImg = false;
        } else {
            ImageManager.bindImageCenterCrop(this, igBanner, doubanTvPojo.getCover());
            isHeadImg = true;
        }

        title.setText(doubanTvPojo.getTitle());
    }

    @Override
    protected void onResume() {
        super.onResume();
        mGridViewClickType = "null";
    }
}
